# BibTeX test case runner for Bibus. 
import BibTeX
import BIB
import _bibtex
import os, sys
#
def compare(file):
    print file
    f = open(file,'r')
    b = _bibtex.open_file(file,0)
    i = BibTeX.importRef(f)
    count = 0
    for x1 in i:
        x2 = _bibtex.next(b)
        count += 1
        for key in x2[4]:
            if BibTeX.importRef.Fields.has_key(key): # Skip keys we know we have no way of dealing with
#                f1 = x1[MAP[key]][1:-1]
                f1 = x1[BibTeX.importRef.Fields[key]]
                f2 = _bibtex.get_native(x2[4][key])
                if (f2[0] == '{' and f2[-1] == '}') or (f2[0] == '"' and f2[-1] == '"'):
                    f2 = f2[1:-1]
                if f1 != f2:
                    print "Mismatch on key",key,f1,f2
            else:
                print "Key %s not handled"%key
    print "Count for %s is %d"%(file,count)
    
for file in os.listdir('../files'):
    try:
        compare('../files/'+file)
    except:
        print "Error reading file", file, sys.exc_info()[0]
#compare('../files/MITI-Literatur00.bib')